package yx.action;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.RelationshipServiceInt;
import yx.vo.JsonVO;
import yx.vo.RelationshipVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "relationshipAction")
public class RelationshipAction extends BaseAction implements ModelDriven<RelationshipVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(RelationshipAction.class);

	private RelationshipVO relationship = new RelationshipVO();

	@Override
	public RelationshipVO getModel() {
		return relationship;
	}

	private RelationshipServiceInt relationshipService;

	public RelationshipServiceInt getRelationshipService() {
		return relationshipService;
	}

	@Autowired
	public void setRelationshipService(RelationshipServiceInt relationshipService) {
		this.relationshipService = relationshipService;
	}

	public void queryRelationship() {
		/*
		 * TODO：需要调查为什么加判定
		 */
		if (relationship.getStudentId() != null) {
			super.writeJson(relationshipService.datagrid(relationship));
		}
	}

	public void addRelationship() {
		JsonVO j = new JsonVO();
		try {
			RelationshipVO u = relationshipService.save(relationship);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeRelationship() {
		relationshipService.remove(relationship.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editRelationship() {
		RelationshipVO u = relationshipService.edit(relationship);
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		j.setObj(u);
		super.writeJson(j);
	}

}
